#include <bits/stdc++.h>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 2e5 + 10;

char s[N];
int n, m;

struct node {
	int s, mxl, mnl, mxr, mnr, res, ml, mr, tot;
} val[N << 2];

node operator + (node a, node b) {
	node c; c.s = a.s + b.s;
	c.mxl = max(a.mxl, a.s + b.mxl); c.mxr = max(b.mxr, b.s + a.mxr);
	c.mnl = min(a.mnl, a.s + b.mnl); c.mnr = min(b.mnr, b.s + a.mnr);
	c.ml = max({ a.ml, b.ml - a.s, a.tot + b.mxl }); c.mr = max({ b.mr, b.s + a.mr, b.tot - a.mnr });
	c.tot = max(a.tot + b.s, b.tot - a.s); c.res = max({ a.res, b.res, b.ml - a.mnr, a.mr + b.mxl });
	return c;
}

void init(int x, int v) {
	val[x].s = v; val[x].mxl = val[x].mxr = max(v, 0);
	val[x].mnl = val[x].mnr = min(v, 0);
	val[x].ml = val[x].mr = val[x].tot = val[x].res = 1;
}

void build(int o = 1, int l = 1, int r = n) {
	if(l == r) return init(o, s[l] == '(' ? 1 : -1); int mid = l + r >> 1;
	build(o << 1, l, mid); build(o << 1 | 1, mid + 1, r); val[o] = val[o << 1] + val[o << 1 | 1];
}

void upd(int pos, int o = 1, int l = 1, int r = n) {
	if(l == r) return init(o, s[l] == '(' ? 1 : -1); int mid = l + r >> 1;
	if(pos <= mid) upd(pos, o << 1, l, mid); else upd(pos, o << 1 | 1, mid + 1, r);
	val[o] = val[o << 1] + val[o << 1 | 1]; 
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in - 1, m = in; n <<= 1;
	scanf("%s", s + 1); 
	build(); printf("%d\n", val[1].res);
	while(m--) {
		int x = in, y = in;
		if(s[x] != s[y]) {
			swap(s[x], s[y]);
			upd(x); upd(y);
		} printf("%d\n", val[1].res);
	}
	return 0;
}

